#!/usr/bin/ruby
$:.unshift File.join(File.dirname(__FILE__))
require 'AWS/EC2'
require 'basic_configuration'
require 'aws_context'
require 'pp'

# Create a keypair and store it in a local file.
# The keypair will also be stored in AWS, and can be used to start instances.

if ARGV[0] == '-?' || ARGV.size != 1 then
   puts "usage: create-keypair name"
   exit 1
end
name=ARGV[0]
# Strip off pathname and .pem
name = File.basename(name, ".pem")

Signal.trap("INT") do
  exit 2
end

config = BasicConfiguration.new
ec2 = AwsContext.instance.ec2(*config.keys)
begin
  keypair = ec2.create_keypair(name, true)
  #pp keypair
  puts "stored keypair in #{keypair[:file_name]}"
rescue
  puts "error #{$!} #{name}"
  exit 1
end
